package com.ict.web.msg.util;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Maps;
import com.ict.common.utils.SecurityUtils;
import com.ict.web.msg.domain.SysMsg;
import com.ict.web.msg.service.ISysMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author xc
 */
@Component
public class CreateMsgUtil {
    @Autowired
    private ISysMsgService sysMsgService;
    private static ISysMsgService mySysMsg;

    @PostConstruct
    public void init() {
        mySysMsg = sysMsgService;
    }
    public final static String PERSON = "您的";
    public final static String SUBMITTED = "%s已提交";

    public final static String DECLARATION_SUBMISSION_SUBMITTED_TITLE= "年度计划上报";

    public final static String ANNUAL_PLAN_TO_DO= "年度计划待办";


    public final static String APPROVAL_RESULT_PASSED = "2";
    public final static String APPROVAL_RESULT_REJECT = "3";
    public final static String APPROVAL_RESULT_MODIFICATION = "4";
    public final static String APPROVAL_RESULT_TITLE= "项目审批更新";

    public final static Map<String,String> openToSocialMap = Maps.newHashMap();
    static{
        openToSocialMap.put(APPROVAL_RESULT_REJECT,"%s审批结果为：驳回，请注意查看并进行下一步操作。");
        openToSocialMap.put(APPROVAL_RESULT_MODIFICATION,"%s审批结果为：退回修改，请注意查看并进行下一步操作。");
        openToSocialMap.put(APPROVAL_RESULT_PASSED,"%s审批结果为：已通过，请注意查看并进行下一步操作。");
    }
    public final static String CONTRACT_FILING_SUBMITTED= "合同备案。";
    public final static String CONTRACT_FILING_SUBMITTED_TITLE= "合同备案更新";

    public final static String MILESTONE_MANAGEMENT_SUBMITTED = "里程碑备案。";
    public final static String MILESTONE_MANAGEMENT_SUBMITTED_TITLE = "里程碑备案更新";

    public final static String COMPLIANCE_MANAGEMENT_SUBMITTED = "合规审查备案。";
    public final static String COMPLIANCE_MANAGEMENT_SUBMITTED_TITLE = "合规审查备案更新";


    public final static String PRELIMINARY_INSPECTION_AND_FILING_SUBMITTED= "初验备案。";
    public final static String PRELIMINARY_INSPECTION_AND_FILING_SUBMITTED_TITLE= "初验备案更新";

    public final static String FINAL_ACCEPTANCE_RECORD_SUBMITTED = "终验备案。";
    public final static String FINAL_ACCEPTANCE_RECORD_SUBMITTED_TITLE = "终验备案更新";

    public final static String ACCEPT_TASKED = "已接受评审任务。";
    public final static String SCHEME_TASKED = "%s方案评审：";
    public final static String TASKED = "%s评审：";
    public final static String TASKED_TITLE = "评审任务已接受";

    public final static String DECLARATION_SUBMISSION_SUBMITTED = "提交项目申报";
    public final static String APPROVAL_RESULT = "审核结果:";
    public final static String PASS_DO_STH = "催办:";
    public final static String PASS_DO = "的验收结果未备案，请您尽快完成备案";
    public final static String PASS_DO_STH_TITLE = "催办";
    /**
     * 生成消息
     *
     * @param type          操作类型
     * @param receiveUserId 消息收件人
     * @param userName      用户
     * @param title         资源名
     */
    public static void createMsg(String type, Long receiveUserId, String userName, String title,String resourceType,String resourceId, Long planId) {
        mySysMsg.save(createSysMsg(type,receiveUserId,userName,title,resourceType,resourceId, planId));
    }
    public static SysMsg createSysMsg(String type, Long receiveUserId, String userName, String title, String resourceType, String resourceId, Long planId){
        if(StringUtils.isBlank(userName)){
            userName = "";
        }
        if(StringUtils.isBlank(title)){
            title = "";
        }
        SysMsg bean = new SysMsg();
        bean.setId(IdWorker.getIdStr());
        bean.setResourceType(resourceType);
        bean.setResourceId(resourceId);
        bean.setReceiveUserId(receiveUserId);
        bean.setTitle(title);
        bean.setContent(String.format(type,userName));
        bean.setCreateTime(LocalDateTime.now());
        bean.setCreateBy(SecurityUtils.getUserId()!=null?String.valueOf(SecurityUtils.getUserId()):"1");
        bean.setMsgStatus(SysMsg.STATUS_NORMAL);
        bean.setReadFlag(SysMsg.READ_FLAG_NORMAL);
        bean.setPlanId(planId);
        return bean;
    }
    /**
     * H5生成消息
     *
     * @param type          操作类型
     * @param receiveUserId 消息收件人
     * @param userName      用户
     * @param title         资源名
     */
    public static void createMsgH5(String type, Long receiveUserId, String userName, String title,String resourceType,String resourceId, Long planId) {
        mySysMsg.save(createSysMsgH5(type,receiveUserId,userName,title,resourceType,resourceId, planId));
    }
    public static SysMsg createSysMsgH5(String type, Long receiveUserId, String userName, String title, String resourceType, String resourceId, Long planId){
        if(StringUtils.isBlank(userName)){
            userName = "";
        }
        if(StringUtils.isBlank(title)){
            title = "";
        }
        SysMsg bean = new SysMsg();
        bean.setId(IdWorker.getIdStr());
        bean.setResourceType(resourceType);
        bean.setResourceId(resourceId);
        bean.setReceiveUserId(receiveUserId);
        bean.setTitle(title);
        bean.setContent(String.format(type,userName));
        bean.setCreateTime(LocalDateTime.now());
        bean.setCreateBy("1");
        bean.setMsgStatus(SysMsg.STATUS_NORMAL);
        bean.setReadFlag(SysMsg.READ_FLAG_NORMAL);
        bean.setPlanId(planId);
        return bean;
    }

    /**
     * 生成公告消息
     *
     * @param type          操作类型
     * @param receiveUserId 消息收件人
     * @param userName      用户
     * @param title         资源名
     */
    public static void createMsgAndNotice(String content, Long receiveUserId, String title,String resourceType,String resourceId, Long noticeId, String fileId) {
        mySysMsg.save(createSysMsgAndNotice(content,receiveUserId,title,resourceType,resourceId,noticeId,fileId));
    }
    public static SysMsg createSysMsgAndNotice(String content, Long receiveUserId, String title, String resourceType, String resourceId, Long noticeId,String fileId){
        if(StringUtils.isBlank(content)){
            content = "";
        }
        if(StringUtils.isBlank(title)){
            title = "";
        }
        SysMsg bean = new SysMsg();
        bean.setId(IdWorker.getIdStr());
        bean.setResourceType(resourceType);
        bean.setResourceId(resourceId);
        bean.setReceiveUserId(receiveUserId);
        bean.setTitle(title);
        bean.setContent(String.format(content));
        bean.setCreateTime(LocalDateTime.now());
        bean.setCreateBy(SecurityUtils.getUserId()!=null?String.valueOf(SecurityUtils.getUserId()):"1");
        bean.setMsgStatus(SysMsg.STATUS_NORMAL);
        bean.setReadFlag(SysMsg.READ_FLAG_NORMAL);
        bean.setNoticeId(noticeId);
        bean.setFileId(fileId);
        return bean;
    }

    /**
     * 生成年度计划消息
     *
     * @param type          操作类型
     * @param receiveUserId 消息收件人
     * @param userName      用户
     * @param title         资源名
     */
    public static void createMsgAnnualProjectPlan(String type, Long receiveUserId, String userName, String title,String resourceType,String resourceId, Long planId) {
        mySysMsg.save(createSysMsgAnnualProjectPlan(type,receiveUserId,userName,title,resourceType,resourceId, planId));
    }
    public static SysMsg createSysMsgAnnualProjectPlan(String type, Long receiveUserId, String userName, String title, String resourceType, String resourceId, Long planId){
        if(StringUtils.isBlank(userName)){
            userName = "";
        }
        if(StringUtils.isBlank(title)){
            title = "";
        }
        SysMsg bean = new SysMsg();
        bean.setId(IdWorker.getIdStr());
        bean.setResourceType(resourceType);
        bean.setResourceId(resourceId);
        bean.setReceiveUserId(receiveUserId);
        bean.setTitle(title);
        bean.setContent(String.format(type,userName));
        bean.setCreateTime(LocalDateTime.now());
        bean.setCreateBy("1");
        bean.setMsgStatus(SysMsg.STATUS_NORMAL);
        bean.setReadFlag(SysMsg.READ_FLAG_NORMAL);
        bean.setPlanId(planId);
        return bean;
    }
}
